//
//  YanCNewFeatureViewController.m
//  ColorfulEnergy
//
//  Created by YanCey on 16/6/8.
//  Copyright © 2016年 YanCey. All rights reserved.
//

#import "YanCNewFeatureViewController.h"
#import "LoginViewController.h"
#import "SMPageControl.h"
#import "BaseTabBarController.h"

#define newFeatrueImageCount 3


@interface YanCNewFeatureViewController () <UIScrollViewDelegate>

@property (nonatomic, weak) SMPageControl *pageControll;

@end

@implementation YanCNewFeatureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = white_;
    
    [self setupScrollView];
    [self setupPageControl];
    
}

//添加UIScrollView
- (void)setupScrollView {
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = self.view.bounds;
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
    
    //给scrollView 添加图片
    CGFloat imageW = scrollView.width;
    CGFloat imageH = scrollView.height;
    for (int i = 0; i < newFeatrueImageCount; i ++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"newFeatures.bundle/newFeatures-index-0%d",i]];
        
        [scrollView addSubview:imageView];
        //设置Frame
        imageView.originY = 0;
        imageView.width = imageW;
        imageView.height = imageH;
        imageView.originX = imageW * i;
        
        //处理最后一个imageView
        if (i == newFeatrueImageCount - 1) {
            [self setupLastImageView:imageView];
        }
        
    }
    //设定scrollView的contentSize和其他属性
    scrollView.contentSize = CGSizeMake(newFeatrueImageCount * imageW, imageH);
    scrollView.pagingEnabled  = YES;  //自动分页属性打开
    scrollView.bounces = NO;  //边缘的弹力拉伸效果 关闭
    scrollView.showsHorizontalScrollIndicator = NO; //水平方向的滚动条指示器关闭
    
}

//处理最后一个ImageView
- (void)setupLastImageView:(UIImageView *)imageView
{
    imageView.userInteractionEnabled = YES;
    //添加开始按钮
    UIButton *startButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    startButton.frame = CGRectMake(0.25 * kScreenW, 0.6 * kScreenH, 0.5 * kScreenW, 50);
    startButton.backgroundColor = [UIColor greenColor];
    startButton.layer.cornerRadius = 10;
    startButton.clipsToBounds = YES;
    
    [startButton setTitle:@"立即体验" forState:(UIControlStateNormal)];
    
    [startButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    startButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    startButton.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    [imageView addSubview:startButton];
    [startButton addTarget:self action:@selector(startUsing) forControlEvents:(UIControlEventTouchUpInside)];

}

- (void)startUsing
{
//    LoginViewController *loginVC = [[LoginViewController alloc] init];
//    RTRootNavigationController *navi = [[RTRootNavigationController alloc] initWithRootViewController:loginVC];
//    [UIApplication sharedApplication].delegate.window.rootViewController = navi;
    
    BaseTabBarController *tabbarVC = [[BaseTabBarController alloc] init];
    [UIApplication sharedApplication].delegate.window.rootViewController = tabbarVC;
    
}

//添加UIPageControl
- (void)setupPageControl
{
    SMPageControl *pageControl = [[SMPageControl alloc] initWithFrame:CGRectMake(0, 0, 0.5 * kScreenW, 30)];
    pageControl.indicatorMargin = 15.0f;
    pageControl.indicatorDiameter = 10.0f;
    pageControl.numberOfPages = newFeatrueImageCount;
    [pageControl setCurrentPage:0];
    pageControl.center = CGPointMake(self.view.width * 0.5, self.view.height * 0.95);
    //设置pageControl的其他属性
    [pageControl setPageIndicatorTintColor:white_];
    [pageControl setCurrentPageIndicatorTintColor:white_];
    pageControl.hidden = YES; //隐藏掉就行了
    [self.view addSubview:pageControl];
    self.pageControll = pageControl;
}

#pragma mark -----UIScrollViewDelegate 

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat doublePage = scrollView.contentOffset.x/scrollView.width;
    int intPage = (int)(doublePage + 0.5); //四舍五入
    self.pageControll.currentPage = intPage;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
